(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:CustomsorderCtrl
     * @description
     * # CustomsorderCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('CustomsorderCtrl', CustomsorderCtrl);

    CustomsorderCtrl.$inject = ['$scope', 'dataService', 'helper', '$rootScope', '$timeout', 'sessionService', 'cacheService', 'Upload', '$location'];

    function CustomsorderCtrl($scope, dataService, helper, $rootScope, $timeout, sessionService, cacheService, Upload, $location) {

        $scope.infoAction = function() {
            $scope.oid = $location.search().order_id;
            $scope.getcustomsnotifys($scope.oid);
        }
        $scope.getcustomsnotifys = function(id) {
            var query = {
                order_id: id
            };
            dataService.getCustomsnotifys(query, 0).then(function(result) {
                if (result.order_data) {
                    $scope.customs_notifys = result.order_data;
                }
            });
        }

        //发货点修改回调方法
        $scope.backpickpoint = function(pickpoint) {
            if (pickpoint) {
                dataService.customsUpdataPickPoint(pickpoint.order_id, pickpoint.pickpoint_id).then(function(result) {
                    if (result.Msg == "ok") {
                        $scope.pickpoint_id = pickpoint.pickpoint_id;
                        helper.message("修改成功！");
                        $scope.getOrderList();
                    } else {
                        helper.message("出错：" + result.Msg);
                        return false;
                    }
                });
            }
        }

        //保存配置
        $scope.syncorder = function() {
            dataService.customsSyncOrder($rootScope.store_id).then(function(a) {
                if (a == "ok") {
                    helper.message("请等待1分钟后，查看新订单");
                } else {
                    helper.message("出错了", "error");
                }
            });
        }

        //保存配置
        $scope.yorder = function() {
            dataService.yorder($rootScope.s_partner_id, $rootScope.store_id, "YUOU").then(function(a) {
                if (a.errCode == "success") {
                    helper.message("请等待1分钟后，查看新订单");
                } else {
                    helper.message("出错了", "error");
                }
            });
        }

        //推送
        $scope.postWms = function(id) {
            dataService.postOederWms(id).then(function(retu) {
                //console.error("retu", retu);
                if (retu.Msg) {
                    helper.message(retu.Msg, "error");
                } else {
                    helper.myAlert("success");
                }
            });
        }

        $scope.configaddAction = function() {
            var xid = $location.search().xid;
            if (xid) {
                dataService.customsConfigOne(xid, $rootScope.store_id).then(function(a) {
                    if (a) {
                        $scope.configData = a;
                    } else {
                        helper.message("非法配置", "error");
                        gourl("#/customsorder/configlist");
                    }
                });
            } else {
                $scope.configData = {
                    store_id: $rootScope.store_id,
                    config: {}
                };
            }
        }

        //保存配置
        $scope.configAdd = function() {
            dataService.customsConfigAdd($scope.configData).then(function(r) {
                if (r) {
                    if (r.Msg) {
                        helper.message(r.Msg, "error");
                    } else {
                        helper.message("创建成功！");
                        gourl("#/customsorder/configlist");
                    }
                }
            });
        }


        //删除配置
        $scope.delConfig = function(id) {
            helper.myConfirm("确认删除？", function() {
                dataService.customsConfigDel(id, $rootScope.store_id).then(function(r) {
                    if (r) {
                        if (r.Msg) {
                            helper.message(r.Msg, "error");
                        } else {
                            helper.message("删除成功！");
                            $scope.configlistAction();
                        }
                    }
                });
            })
        }

        //显示 
        $scope.configlistAction = function() {
            $scope.pageSize = 10;
            $scope.orlist = [];
            $scope.getConfigList(1);
        }

        //获取列表
        $scope.getConfigList = function(pageNo) {
            var query = {};
            query = {
                page_no: pageNo,
                page_size: $scope.pageSize,
            };

            //门店
            query.store_id = $scope.store_id;

            $scope.configList = [];
            $scope.ordersTotal = 0;
            dataService.customsConfigGet(query).then(function(list) {
                if (list) {
                    $scope.configList = list.config_list;
                    $scope.configTotal = list.total_count;
                }
            });
        }

        //显示 
        $scope.listAction = function() {
            $scope.chosedStatus = "S_ORDER_WAIT_DELIVERY";
            $scope.pageSize = 10;
            $scope.pageNo = 1;
            $scope.orlist = [];
            $scope.getOrderList(1);

            $scope.$on('event:up-store-id', function() {
                $scope.store_id = $rootScope.store_id;
                $scope.getOrderList(1);
            });

            //监控选中
            $scope.$watch('fhmoder', function(newValue, oldValue) {
                if (newValue) {
                    var order_ids = [];
                    for (var i in newValue) {
                        order_ids.push(newValue[i].order_id);
                    }
                    $scope.selectedOrderIds = order_ids.join("\n");
                }
            }, true);
        }

        $scope.uncheckAll = function() {
            $scope.fhmoder = [];
        };

        $scope.checkAll = function() {
            if ($scope.idtype) {
                $scope.fhmoder = angular.copy($scope.orderslist);
                $scope.idtype = 0;
            } else {
                $scope.fhmoder = [];
                $scope.idtype = 1;
            }
        };

        //tab点击
        $scope.tabClick = function() {
            $scope.pageNo = 1;
            $scope.getOrderList();
        }

        //获取列表
        $scope.getOrderList = function(pageNo) {
            var query = {};
            query = {
                page_no: pageNo ? pageNo : $scope.pageNo,
                page_size: $scope.pageSize,
                status: $scope.chosedStatus
            };

            $scope.pageNo = query.page_no;

            //门店
            query.store_id = $rootScope.store_id;

            if ($scope.pickpoint_id) {
                query.pickpoint_id = $scope.pickpoint_id;
            }

            if ($scope.order_id) {
                query.order_id = $scope.order_id;
            }

            if ($scope.start_created && $scope.end_created) {
                query.start_created = $scope.start_created;
                query.end_created = $scope.end_created;
            }

            $scope.orderslist = [];
            $scope.ordersTotal = 0;
            dataService.customsOrderGet(query).then(function(list) {
                if (list) {
                    $scope.orderslist = list.order_list;
                    $scope.ordersTotal = list.total_count;
                }
            });
        }

        // 推送订单之前判断是否是淘宝订单，并且判断是否已经支付过
        $scope.operationOrder = function(id, type, order_from) {
            if (!id) {
                helper.message("没有订单号，无法推单", "error");
                return false;
            }

            if (!order_from) {
                helper.message("没有订单来源，无法推单", "error");
                return false;
            } else {
                // 淘宝[没有支付单的类型]
                if (order_from == "TB") {
                    var query = {
                        "store_id": $rootScope.store_id,
                        "order_id": id
                    };

                    dataService.customsOrderGet(query).then(function(list) {
                        if (list) {
                            var order_info = list.order_list[0];
                            if (order_info.status == "S_ORDER_WAIT_PAYMENT") {
                                helper.message("没有支付单，请先补缺支付单之后，再推送订单");
                                return false;
                            } else {
                                $scope.postOrder(id, type, order_from);
                            }
                        } else {
                            helper.message("订单信息不存在，无法推单", "error");
                            return false;
                        }
                    });
                } else {
                    // 孩子王 渝欧[有支付单或者人家帮忙推送过支付单]
                    $scope.postOrder(id, type, order_from);
                }
            }
        }

        //推送订单
        $scope.postOrder = function(id, type, order_from) {
            dataService.postOrder(id, type, order_from).then(function(retu) {
                if (retu.Msg) {
                    helper.message(retu.Msg, "error");
                } else {
                    helper.message("推送成功");
                }
            });
        }

        // //渝欧确定订单
        // $scope.confirmYuou = function(order_id) {
        //
        //     if(!order_id)
        //     {
        //         helper.message("没有获取到订单号", "error");
        //         return false;
        //     }
        //     var data = {
        //         orderNo:order_id
        //         };
        //     dataService.confirmYuou(data).then(function(retu) {
        //         if (retu.errCode == "success") {
        //             helper.message(retu.msg);
        //         } else {
        //             helper.message(retu.msg, "error");
        //         }
        //     });
        // }

        //渝欧确定订单
        $scope.lackpaybill = function(order_id) {
            if (!order_id) {
                helper.message("没有获取到订单号", "error");
                return false;
            }
            var data = {
                orderNo: order_id
            };
            dataService.lackpaybill(data).then(function(retu) {
                if (retu.errCode == "success") {
                    helper.message(retu.msg);
                } else {
                    helper.message(retu.msg, "error");
                }
            });
        }

        //外部订单作废
        $scope.zuofei = function(order_id) {
            helper.myConfirm("确认操作么？", function() {
                if (!order_id)
                {
                    helper.message("没有获取到订单号", "error");
                    return false;
                }
                var data = {
                    orderNo: order_id,
                    partner_id:$rootScope.s_partner_id,
                    store_id:$rootScope.store_id
                };
                dataService.zuofei(data).then(function(retu) {
                    if (retu.errCode == "success") {
                        helper.message(retu.msg);
                    } else {
                        helper.message(retu.msg, "error");
                    }
                });
            });
        }

        //推送支付单
        $scope.postPay = function(id, order_from) {
            return dataService.postPay(id, order_from).then(function(retu) {
                if (retu.Msg) {
                    helper.message(retu.Msg, "error", 60);
                } else {
                    helper.myAlert("success", null, null, function() {
                        // $scope.getorderList();
                    });
                }
                return retu;
            });
        }

        // $scope.YuouWuliu = function ()
        // {
        //     dataService.YuouWuliu().then(function(retu) {
        //         if (retu.errCode == "success")
        //         {
        //             helper.message(retu.msg);
        //         }
        //         else
        //         {
        //             helper.message(retu.msg, "error");
        //         }
        //     });
        // }
    }
})();